/**
 * 商品分类
 * */


var plist = [];


(function () {
})();


//分类列表
function getList(){
    $.ajax({
        type: apis.findCateList.method,
        url: apis.findCateList.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].pname + '</td>';
                h += '<td>' + d[i].cate_name + '</td>';
                h += '<td>' + d[i].uptime + '</td>';
                h += '<td class="f-14">';
                h += '<a href="javascript:;" onclick="find_edit(\'编辑\',\'find-cate-edit.html\', ' + d[i].id + ');" style="text-decoration: none">';
                h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
                h += '</a>';
                h += ' &nbsp; ';
                h += '<a href="javascript:;" onclick="find_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
                h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
                h += '</a>';
                h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].cate_name + '"/>';
                h += '<input type="hidden" id="pid' + d[i].id + '" value="' + d[i].pid + '"/>';
                h += '</td>';
                h += '</tr>';
            }
            $('tbody').html(h);
            $('#pageTotal').html(d.length);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//编辑
function find_edit(title,url,id,w,h){
    url += '?id=' + id;
    var title = $('#title' + id).val();
    var pid = $('#pid' + id).val();
    title = title ? title : '';
    url += '&title=' + escape(title);
    url += '&pid=' + pid;
    layer_show(title,url,w,500);
}


//更新
function modify(){
    var formData = {
        id: id,
        title: $('#title').val(),
        pid: $('#pid').val(),
    };
    console.log(formData);
    if (!formData.title) {
        //layer.msg('SKU规格必须的',{icon: 0,time: 1000});
        msgPop({msg: '名称必须的'});
        return
    }
    //return
    $.ajax({
        type: apis.findCateModify.method,
        url: apis.findCateModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '更新成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//删除
function find_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.findCateDel.method,
            url: apis.findCateDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    //msgPop({msg: res.msg});
                    layerMsgErr(res.msg);
                    return
                }
                //layer.msg('商品已删除!',{icon: 1,time: 1000});
                layerMsgOk('删除成功');
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}